package main.java.indi.zyj.hot100;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class _51_generateParenthesis {

    List<String> res = new ArrayList<>();
    StringBuilder path = new StringBuilder();

    public List<String> generateParenthesis(int n) {
        if (n == 0) return res;
        backtrack(n, n);
        return res;
    }

    private void backtrack(int left, int right) {
        if (left > right) return;
        if (left < 0 || right < 0) return;
        if (left == 0 && right == 0) {
            res.add(path.toString());
            return;
        }

        path.append('(');
        backtrack(left - 1, right);
        path.deleteCharAt(path.length() - 1);

        path.append(')');
        backtrack(left, right - 1);
        path.deleteCharAt(path.length() - 1);
    }


}
